package com.example.studyroom.controller;
/*
    超级管理员功能控制类
*/

import com.example.studyroom.Entity.StudyRoom;
import com.example.studyroom.Entity.StudyRoomAdmin;
import com.example.studyroom.Entity.SuperAdmin;
import com.example.studyroom.Entity.User;
import com.example.studyroom.dto.StudyRoomAdmin.StudyRoomAdminDTO;
import com.example.studyroom.dto.StudyRoom.StudyRoomDTO;
import com.example.studyroom.dto.SuperAdmin.SuperAdminLoginDTO;
import com.example.studyroom.dto.StudyRoomAdmin.addStudyRoomAdminDTO;
import com.example.studyroom.dto.StudyRoom.addStudyRoomDTO;
import com.example.studyroom.dto.User.UserDTO;
import com.example.studyroom.dto.User.UserLoginDTO;
import com.example.studyroom.service.StudyRoomAdminService;
import com.example.studyroom.service.StudyRoomService;
import com.example.studyroom.service.SuperAdminService;
import com.example.studyroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/superadmin")
public class SuperAdminController {

    @Autowired
    private SuperAdminService superAdminService; // 注入SuperAdminService依赖

    @Autowired
    private UserService userService; // 注入UserService依赖

    @Autowired
    private StudyRoomAdminService studyRoomAdminService; // 注入StudyRoomAdminService依赖

    @Autowired
    private StudyRoomService studyRoomService; // 注入StudyRoomService依赖

    /**
     * 超级管理员登录
     *
     * @param superAdminLoginDTO 包含超级管理员登录信息的 DTO
     * @return 成功登录返回 "登录成功!"，否则返回 "登录失败"
     */
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody SuperAdminLoginDTO superAdminLoginDTO) {
        SuperAdmin existingSuperAdmin = superAdminService.findBySuperAdminLogin(superAdminLoginDTO.getSname(), superAdminLoginDTO.getSpass());
        if (existingSuperAdmin != null) {
            return ResponseEntity.ok("登录成功!");
        } else {
            return ResponseEntity.badRequest().body("登录失败");
        }
    }

    /**
     * 获取所有用户信息
     *
     * @return 返回所有用户的列表
     */
    @GetMapping("/alluser")
    public List<UserDTO> getAllUsers() {
        List<User> users = userService.getAllUsers(); // 从数据库获取原始数据
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            UserDTO userDTO = new UserDTO();
            userDTO.setUid(user.getUid());
            userDTO.setUname(user.getUname());
            userDTO.setUpass(user.getUpass());
            userDTO.setPhone(user.getPhone());
            userDTO.setGender(user.getGender());
            userDTO.setAge(user.getAge());
            userDTO.setHobby(user.getHobby());
            userDTO.setAddress(user.getAddress());

            userDTOs.add(userDTO);
        }
        return userDTOs;
    }

    /**
     * 删除用户
     *
     * @param uid 用户ID
     * @return 成功删除返回状态200
     */
    @DeleteMapping("/deleteuser/{uid}")
    public ResponseEntity<Void> deleteUser(@PathVariable Integer uid) {
        userService.deleteUser(uid);
        return ResponseEntity.ok().build();
    }

    /**
     * 重置用户密码
     *
     * @param uid  用户ID
     * @param user 包含新密码的用户对象
     */
    @PutMapping("/modifypass/{uid}")
    public void modifyPass(@PathVariable Integer uid, @RequestBody User user) {
        user.setUid(uid);
        userService.modifyPass(user);
    }

    /**
     * 获取所有自习室管理员信息
     *
     * @return 返回所有自习室管理员的列表
     */
    @GetMapping("/allstudyroomadmin")
    public List<StudyRoomAdminDTO> getAllStudyRoomAdmins() {
        List<StudyRoomAdmin> studyRoomAdmins = studyRoomAdminService.getAllStudyRoomAdmins(); // 从数据库获取原始数据
        List<StudyRoomAdminDTO> studyRoomAdminDTOs = new ArrayList<>();
        for (StudyRoomAdmin studyRoomAdmin : studyRoomAdmins) {
            StudyRoomAdminDTO studyRoomAdminDTO = new StudyRoomAdminDTO();
            studyRoomAdminDTO.setRid(studyRoomAdmin.getRid());
            studyRoomAdminDTO.setRname(studyRoomAdmin.getRname());
            studyRoomAdminDTO.setRpass(studyRoomAdmin.getRpass());
            studyRoomAdminDTO.setRoom(studyRoomAdmin.getRoom());

            studyRoomAdminDTOs.add(studyRoomAdminDTO);
        }
        return studyRoomAdminDTOs;
    }

    /**
     * 添加自习室管理员
     *
     * @param addStudyRoomAdminDTO 包含自习室管理员信息的 DTO
     * @return 成功添加返回状态200
     */
    @PostMapping("/addstudyroomadmin")
    public ResponseEntity<Void> addStudyRoomAdmin(@RequestBody addStudyRoomAdminDTO addStudyRoomAdminDTO) {
        StudyRoomAdmin studyRoomAdmin = new StudyRoomAdmin()
                .setRname(addStudyRoomAdminDTO.getRname())
                .setRpass(addStudyRoomAdminDTO.getRpass())
                .setRoom(addStudyRoomAdminDTO.getRoom());
        studyRoomAdminService.addStudyRoomAdmin(studyRoomAdmin);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改自习室管理员信息
     *
     * @param id             自习室管理员ID
     * @param studyRoomAdmin 包含新信息的自习室管理员对象
     */
    @PutMapping("/modifystudyroomadmin/{id}")
    public void modifyStudyRoomAdmin(@PathVariable Integer id, @RequestBody StudyRoomAdmin studyRoomAdmin) {
        studyRoomAdmin.setRid(id);
        studyRoomAdminService.modifyStudyRoomAdmin(studyRoomAdmin);

        // 更新自习室表中负责的管理员信息
        StudyRoom studyRoom = studyRoomService.findByRoomName(studyRoomAdmin.getRoom());
        if (studyRoom != null) {
            studyRoom.setAdminName(studyRoomAdmin.getRname());
            studyRoomService.updateRoomAdmin(studyRoom);
        } else {
            // 处理自习室不存在的情况，可以抛出异常或者进行其他处理
        }
    }

    /**
     * 删除自习室管理员
     *
     * @param rid 自习室管理员ID
     * @return 成功删除返回状态200
     */
    @DeleteMapping("/deletestudyroomadmin/{rid}")
    public ResponseEntity<Void> deleteStudyRoomAdmin(@PathVariable Integer rid) {
        studyRoomAdminService.deleteStudyRoomAdmin(rid);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取所有自习室信息
     *
     * @return 返回所有自习室的列表
     */
    @GetMapping("/allstudyroom")
    public List<StudyRoomDTO> getAllStudyRooms() {
        List<StudyRoom> studyRooms = studyRoomService.getAllStudyRooms(); // 从数据库获取原始数据
        List<StudyRoomDTO> studyRoomDTOs = new ArrayList<>();
        for (StudyRoom studyRoom : studyRooms) {
            StudyRoomDTO studyRoomDTO = new StudyRoomDTO();
            studyRoomDTO.setId(studyRoom.getId());
            studyRoomDTO.setRoomName(studyRoom.getRoomName());
            studyRoomDTO.setCapacity(studyRoom.getCapacity());
            studyRoomDTO.setFacilities(studyRoom.getFacilities());
            studyRoomDTO.setAdminName(studyRoom.getAdminName());

            studyRoomDTOs.add(studyRoomDTO);
        }
        return studyRoomDTOs;
    }

    /**
     * 添加自习室
     *
     * @param addStudyRoomDTO 包含自习室信息的 DTO
     * @return 成功添加返回状态200
     */
    @PostMapping("/addstudyroom")
    public ResponseEntity<Void> addStudyRoom(@RequestBody addStudyRoomDTO addStudyRoomDTO) {
        StudyRoom studyRoom = new StudyRoom()
                .setRoomName(addStudyRoomDTO.getRoomName())
                .setCapacity(addStudyRoomDTO.getCapacity())
                .setFacilities(addStudyRoomDTO.getFacilities());
        studyRoomService.addStudyRoom(studyRoom);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除自习室
     *
     * @param id 自习室ID
     * @return 成功删除返回状态200
     */
    @DeleteMapping("/deletestudyroom/{id}")
    public ResponseEntity<Void> deleteStudyRoom(@PathVariable Integer id) {
        studyRoomService.deleteStudyRoom(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改自习室信息
     *
     * @param id        自习室ID
     * @param studyRoom 包含新信息的自习室对象
     */
    @PutMapping("/modifystudyroom/{id}")
    public void modifyStudyRoom(@PathVariable Integer id, @RequestBody StudyRoom studyRoom) {
        studyRoom.setId(id);
        studyRoomService.modifyStudyRoom(studyRoom);
    }

    /**
     * 获取所有自习室名称
     *
     * @return 返回所有自习室的名称列表
     */
    @GetMapping("/allroomname")
    public List<StudyRoomDTO> getAllRoomName() {
        List<StudyRoom> studyRooms = studyRoomService.getAllStudyRooms(); // 从数据库获取原始数据
        List<StudyRoomDTO> studyRoomDTOs = new ArrayList<>();
        for (StudyRoom studyRoom : studyRooms) {
            StudyRoomDTO studyRoomDTO = new StudyRoomDTO();
            studyRoomDTO.setRoomName(studyRoom.getRoomName());
            studyRoomDTOs.add(studyRoomDTO);
        }
        return studyRoomDTOs;
    }


    /**
     * 获取所有自习室管理员的名称
     * 为了后面新增和修改自习室的相关管理员时做准备
     *
     * @return 返回所有自习室管理员的名称列表
     */
    @GetMapping("/allRname")
    public List<StudyRoomAdminDTO> getAllRname() {
        List<StudyRoomAdmin> studyRoomAdmins = studyRoomAdminService.getAllStudyRoomAdmins(); // 从数据库获取原始数据
        List<StudyRoomAdminDTO> studyRoomAdminDTOs = new ArrayList<>();
        for (StudyRoomAdmin studyRoomAdmin : studyRoomAdmins) {
            StudyRoomAdminDTO studyRoomAdminDTO = new StudyRoomAdminDTO();
            studyRoomAdminDTO.setRname(studyRoomAdmin.getRname());
            studyRoomAdminDTOs.add(studyRoomAdminDTO);
        }
        return studyRoomAdminDTOs;
    }
}
